package com.zookeeper.utils;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * @Author 咕唧
 * @Date 2022/6/29 20:02
 * @Version 1.0
 * zookeeper连接工具类
 */
public class ConnectionUtil {
    //服务器的地址,多个地址以逗号分割
    public static final String SERVER_ADDRESS = "120.48.95.238:2181";

    public static CuratorFramework getFramework() {
        CuratorFramework curatorFramework = CuratorFrameworkFactory.builder()
                //设置连接服务器地址
                .connectString(SERVER_ADDRESS)
                //设置连接超时时间
                .connectionTimeoutMs(20000)
                //会话超时时间
                .sessionTimeoutMs(30000)
                //设置连接失败重试策略 ，当连接失败时，baseSleepTimeMs表示等待一秒钟，可重试次数为 5 次
                .retryPolicy(new ExponentialBackoffRetry(1000, 5)).build();

        curatorFramework.start();
        return curatorFramework;
    }

}
